﻿#include "expressionui.h"
#include "ui_expressionui.h"

ExpressionUI::ExpressionUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ExpressionUI)
{
    ui->setupUi(this);
    m_mapper = new QSignalMapper(this);
    //初始化表情包
    initExpression();
}

ExpressionUI::~ExpressionUI()
{
    delete ui;
}

void ExpressionUI::initExpression()
{
    QDir dir1("D:\\QTCode5.14\\CClient\\res\\bq1");
    QStringList list1 = dir1.entryList(QDir::Files);
    QDir dir2("D:\\QTCode5.14\\CClient\\res\\bq2");
    QStringList list2 = dir2.entryList(QDir::Files);
    QDir dir3("D:\\QTCode5.14\\CClient\\res\\bq3");
    QStringList list3 = dir3.entryList(QDir::Files);
    QStringList list4 = list1+list2+list3;
    int num = 0;
    int count = list4.size()/7;
    for(int j=0;j<count;++j)
    {
        QHBoxLayout* hl = new QHBoxLayout;
        hl->setContentsMargins(0,0,0,0);
        hl->setMargin(0);
        hl->setSpacing(0);
        for(int i = 0;i<7;++i)
        {
            QPushButton* btn = new QPushButton;
            if(num<list1.size())
            {
                QIcon icon(QString("D:\\QTCode5.14\\CClient\\res\\bq1\\%1").arg(list4[num]));
                btn->setIcon(icon);
                m_vec.append(QString("D:\\QTCode5.14\\CClient\\res\\bq1\\%1").arg(list4[num]));
            }
            else if (num>=list1.size()&&num<list2.size()+list1.size())
            {
                QIcon icon(QString("D:\\QTCode5.14\\CClient\\res\\bq2\\%1").arg(list4[num]));
                btn->setIcon(icon);
                m_vec.append(QString("D:\\QTCode5.14\\CClient\\res\\bq2\\%1").arg(list4[num]));
            }
            else if(num>=list2.size()+list1.size())
            {
                QIcon icon(QString("D:\\QTCode5.14\\CClient\\res\\bq3\\%1").arg(list4[num]));
                btn->setIcon(icon);
                m_vec.append(QString("D:\\QTCode5.14\\CClient\\res\\bq3\\%1").arg(list4[num]));
            }
            btn->setFixedSize(50,50);
            btn->setIconSize(QSize(50,50));
            btn->setStyleSheet("background:rgba(0,0,0,0);border:none;");
            connect(btn,SIGNAL(clicked(bool)),m_mapper,SLOT(map()));

            m_mapper->setMapping(btn,num);
            hl->addWidget(btn);
            num++;
        }
        ui->verticalLayout->setContentsMargins(0,0,0,0);
        ui->verticalLayout->setSpacing(0);
        ui->verticalLayout->addLayout(hl);
    }
    connect(m_mapper,QOverload<int>::of(&QSignalMapper::mapped),this,&ExpressionUI::getExpression);
}

ExpressionUI &ExpressionUI::getInstance()
{
    static ExpressionUI ui;
    return ui;
}

void ExpressionUI::getExpression(int i)
{
    qDebug()<<m_vec[i]<<endl;
    emit Expre(m_vec[i]);
}
